﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.OleDb;
using HotelManager.Data_Transfer_Layer;

namespace HotelManager.Data_Access_Layer
{
    class loaiphongDAO
    {
        #region Variables
        private OleDbConnection conn;
        private string error;
        #endregion

        #region Properties
        public string Error
        {
            get { return error; }
            set { error = value; }
        }
        #endregion

        public loaiphongDAO()
        {
            error = "";
            try
            {
                string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=.\\hotel.accdb";
                conn = new OleDbConnection(strConn);
                conn.Open();
            }
            catch (Exception ex)
            {
                Error = "Không kết nối được đến cơ sở dữ liêu";
            }
        }

        public loaiphongDTO[] get()
        {
            loaiphongDTO[] objs;
            objs = new loaiphongDTO[100];
            try
            {
                string sql = "SELECT * FROM loaiphong";
                OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                OleDbDataReader reader = cmd.ExecuteReader();
                int i = 0, n = dt.Rows.Count;
                objs = new loaiphongDTO[n];
                while (reader.Read())
                {
                    objs[i] = new loaiphongDTO();
                    objs[i].Lp_id = reader.GetInt32(0);
                    objs[i].Lp_tenloaiphong = reader.GetString(1).ToString();
                    objs[i].Lp_giaphong = reader.GetString(2).ToString();
                    objs[i].Lp_uudai = reader.GetString(3).ToString();
                    i++;
                }
            }
            catch (Exception ex)
            {

            }
            return objs;
        }

        public string[] get_arr()
        {
            loaiphongDTO[] objs;
            string[] arr_obj = new string[100];
            objs = new loaiphongDTO[100];
            try
            {
                string sql = "SELECT * FROM loaiphong";
                OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                OleDbDataReader reader = cmd.ExecuteReader();
                int i = 0, n = dt.Rows.Count, j;
                objs = new loaiphongDTO[n];
                arr_obj = new string[n + 1];
                while (reader.Read())
                {
                    j = i + 1;
                    objs[i] = new loaiphongDTO();
                    objs[i].Lp_id = reader.GetInt32(0);
                    objs[i].Lp_tenloaiphong = reader.GetString(1).ToString();
                    arr_obj[i] = objs[i].Lp_tenloaiphong;
                    i++;
                }
            }
            catch (Exception ex)
            {

            }
            return arr_obj;
        }

        public loaiphongDTO View_Detail(int lp_id)
        {
            loaiphongDTO obj = new loaiphongDTO();
            try
            {
                string sql = "SELECT * FROM loaiphong WHERE lp_id=" + lp_id;
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    obj.Lp_tenloaiphong = reader.GetString(1).ToString();
                    obj.Lp_giaphong = reader.GetString(2).ToString();
                    obj.Lp_uudai = reader.GetString(3).ToString();
                }
            }
            catch (Exception ex)
            {
                Error = ex.ToString();
            }
            return obj;
        }

        public loaiphongDTO View_DetailbyName(string lp_tenloaiphong)
        {
            loaiphongDTO obj = new loaiphongDTO();
            try
            {
                string sql = "SELECT * FROM loaiphong WHERE lp_tenloaiphong='" + lp_tenloaiphong + "'";
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    obj.Lp_id = reader.GetInt32(0);
                }
            }
            catch (Exception ex)
            {
                Error = ex.ToString();
            }
            return obj;
        }

        public void Delete(loaiphongDTO obj)
        {
            try
            {
                string sql = "DELETE FROM loaiphong WHERE lp_id=" + obj.Lp_id;
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Error = ex.ToString();
            }
        }

        public void Insert(loaiphongDTO obj)
        {
            try
            {
                string sql = "INSERT INTO loaiphong VALUES('','" + obj.Lp_tenloaiphong + "','" + obj.Lp_giaphong + "','" + obj.Lp_uudai + "')";
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Error = ex.ToString();
            }
        }

        public void Update(loaiphongDTO obj)
        {
            try
            {
                string sql1 = "UPDATE loaiphong SET lp_tenloaiphong='" + obj.Lp_tenloaiphong + "',lp_giaphong='" + obj.Lp_giaphong + "',lp_uudai='" + obj.Lp_uudai + "' WHERE lp_id=" + obj.Lp_id;
                OleDbCommand cmd = new OleDbCommand(sql1, conn);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Error = ex.ToString();
            }
        }
    }
}
